package org.thirdHero.admin.web;

import com.github.pagehelper.PageInfo;
import org.thirdHero.admin.annotation.LoginAdmin;
import org.thirdHero.core.util.CharUtil;
import org.thirdHero.core.util.ResponseUtil;
import org.thirdHero.core.util.bcrypt.BCryptPasswordEncoder;
import org.thirdHero.db.domain.SysUser;
import org.thirdHero.db.service.SysUserService;
import org.thirdHero.db.vo.SysUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/sysUser")
public class SysUserController {
    private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);
    @Resource
    SysUserService sysUserService;

    @PostMapping("/create")
    @ResponseBody
    public Object create(@LoginAdmin SysUser loginUser,@RequestBody  SysUser sysUser ){
        sysUser.setCreateDate(LocalDateTime.now());
        cryptPassword(sysUser);
        //willAdd: 判断角色信息是否存在；判断部门信息是否存在
        this.sysUserService.saveSysUser(sysUser);
        return ResponseUtil.ok("创建成功");
    }
    @PostMapping("/freeze/{id}/{status}")
    public Object freeze(@LoginAdmin SysUser sysUser ,@PathVariable Integer id,@PathVariable Integer status){
        this.sysUserService.setStatus(id, status);
        return ResponseUtil.ok("操作成功");
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Object deleteSysUser(@LoginAdmin SysUser sysUser ,@PathVariable String id){
        this.sysUserService.deleteSysUser(id);
        return ResponseUtil.ok("删除成功");
    }
        @PutMapping("/editSysUser")
    @ResponseBody
    public Object editSysRole(@LoginAdmin SysUser sysUser ,@RequestBody SysUser userMessage){

        cryptPassword(userMessage);
            this.sysUserService.editSysUser(userMessage);
        return ResponseUtil.ok("更新成功");
    }

    private void cryptPassword(@RequestBody SysUser userMessage) {
        String salt = CharUtil.getRandomString(5);
        String rawPassword = userMessage.getPassword() + salt;
        String encodedPassword = "";
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        encodedPassword = bCryptPasswordEncoder.encode(rawPassword);
        userMessage.setPassword(encodedPassword);
        userMessage.setSalt(salt);
    }

    @PostMapping("/list")
    @ResponseBody
    public Object list(@LoginAdmin SysUser sysUser ,@RequestParam(required = false) String name,
                       @RequestParam(value = "page",defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                       @RequestParam(required = false) String roleId,
                       @RequestParam(required = false) String deptId,
                       @RequestParam(required = false) String phone){
        SysUserVo sysUserVo = new SysUserVo();
        if(!StringUtils.isEmpty(name)){
            sysUserVo.setName(name);
        }
        if(!StringUtils.isEmpty(roleId)){
            sysUserVo.setRoleId(Integer.parseInt(roleId));
        }
        if(!StringUtils.isEmpty(deptId)){
            sysUserVo.setDeptId(Integer.parseInt(deptId));
        }
        if(!StringUtils.isEmpty(phone)){
            sysUserVo.setPhone(phone);
        }
        List<SysUserVo> sysUserVos = sysUserService.querySysUser(sysUserVo, page,limit);
        PageInfo<SysUserVo> pageInfo = new PageInfo<>(sysUserVos);
        ResponseUtil<SysUserVo> responseUtil = new ResponseUtil<SysUserVo>();
        return  responseUtil.ok("成功",pageInfo.getTotal(),sysUserVos);
    }
}
